﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Data.Objects.DataClasses;

using MINERP.DAL;
using MINERP.DAL.InfoConfig;
using MINERP.MODEL.InfoConfig;
namespace MINERP.BLL.InfoConfig
{
    public class UserBLL
    {
        #region //Private Fields
        //private UsersEntity userEntity = null;
        //private List<users>     _conllection = null;
        //private List<UsersEntity> _userEntityList = new List<UsersEntity>();


        #endregion

        public UserBLL()
        {
        }

        #region Public Methods

        public List<users> GetUserList()
        {

            //return UserDAL.GetUserList();
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                List<users> list = entities.users.Where(u => u.IsUseful == true).ToList();

                foreach (var item in list)
                {
                    if (!item.tb_WorkerGroupReference.IsLoaded)
                    {
                        item.tb_WorkerGroupReference.Load();
                    }
                    if (!item.tb_Power.IsLoaded)
                    {
                        item.tb_Power.Load();
                    }
                }
                return list;

            }
        }
        /// <summary>
        /// 获得单个用户及其操作权限
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public users GetUserAndAction(users obj)
        {
            return UserDAL.GetUserAndAction(obj);
        }
        /// <summary>
        ///  获得单个用户及其角色
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public users GetUserAndPower(users obj)
        {
            return UserDAL.GetUserAndPower(obj);
        }
        /// <summary>
        /// 获得一个角色的操作List
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public List<tb_Action> GetActionList(tb_Power obj)
        {
            return UserDAL.GetActionList(obj);
        }
        /// <summary>
        /// 获得用户组list
        /// </summary>
        /// <returns></returns>
        public List<tb_WorkerGroup> GetGroupList()
        {
            return UserDAL.GetGroupList();
        }
        /// <summary>
        /// 获得用户角色list
        /// </summary>
        /// <returns></returns>
        public List<tb_Power> GetPowerList()
        {
            return UserDAL.GetPowerList();
        }
        /// <summary>
        /// 获得单个角色对象
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public tb_Power GetPowetObj(string powerName)
        {
            return UserDAL.GetPowerObj(powerName);
        }
        /// <summary>
        /// 获得单个用户组对象
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public tb_WorkerGroup GetGroupObj(string groupName)
        {
            return UserDAL.GetGroupObj(groupName);
        }
        /// <summary>
        /// 获得单个操作对象
        /// </summary>
        /// <param name="actionName"></param>
        /// <returns></returns>
        public tb_Action GetActionObj(string actionName)
        {
            return UserDAL.GetActionObj(actionName);
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="obj"></param>
        public void AddUser(users obj, List<tb_Action> actionList, List<tb_Tech> techList, List<tb_Power> powerList, string groupName)
        {
            users user = new users();
            foreach (tb_Action action in actionList)
            {
                user.tb_Action.Add(action);
            }
            foreach (var item in techList)
            {
                user.tb_Tech.Add(item);
            }
            foreach (tb_Power power in powerList)
            {
                user.tb_Power.Add(power);
            }
            if (groupName != string.Empty)
            {
                tb_WorkerGroup group = GetGroupObj(groupName);
                user.tb_WorkerGroup = group;
            }
            user.LoginName = obj.LoginName;
            user.LoginPwd = obj.LoginPwd;
            user.HeadPicPath = obj.HeadPicPath;
            user.IsLeader = obj.IsLeader;
            user.IsUseful = obj.IsUseful;
            user.MaxTaskCount = obj.MaxTaskCount;
            user.UserName = obj.UserName;
            user.IsOnline = false;
            user.LastLoginTime = DateTime.Now;
            UserDAL.AddUser(user);
        }
        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="obj"></param>
        public int UpdateUser(users obj, List<tb_Action> actionList, List<tb_Tech> techList, List<tb_Power> powerList, string groupName)
        {
           return  UserDAL.UpdateUser(obj, actionList, techList, powerList, groupName);
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="obj"></param>
        public void DeleteUser(users obj)
        {
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                users user = entities.users.FirstOrDefault(u => u.userID == obj.userID);
                if (user != null)
                {
                    user.IsUseful = false;
                }
                entities.SaveChanges();

            }
        }

        public users GetUserObj(int id)
        {
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                return entities.users.FirstOrDefault(u => u.userID == id);
            }
        }
        #endregion

        public users GetUserByUserName(string userName)
        {
            using (newSampleYPDBEntities entities = new newSampleYPDBEntities())
            {
                var a = entities.users.FirstOrDefault(u => u.UserName == userName);
                return a;
            }
        }
    }


}
